import CreepController from "../controller"


class RoleCenterLabManager {

    static sleep(creep: Creep) {
        creep.say('Zzz')
        const storage = creep.room[STRUCTURE_STORAGE]
        if (storage) {
            if (creep.store[RESOURCE_ENERGY] > 0) {
                const status = creep.transfer(storage, RESOURCE_ENERGY)
                if (status == ERR_NOT_IN_RANGE) {
                    creep.goTo(storage.pos)
                    return
                }
                else if (status == OK) {
                    creep.memory.working = false
                }
            }
            if (creep.store[RESOURCE_GHODIUM_HYDRIDE] > 0) {
                const status = creep.transfer(storage, RESOURCE_GHODIUM_HYDRIDE)
                if (status == ERR_NOT_IN_RANGE) {
                    creep.goTo(storage.pos)
                    return
                }
                else if (status == OK) {
                    creep.memory.working = false
                }
            }
            if (creep.store[RESOURCE_GHODIUM_ACID] > 0) {
                const status = creep.transfer(storage, RESOURCE_GHODIUM_ACID)
                if (status == ERR_NOT_IN_RANGE) {
                    creep.goTo(storage.pos)
                    return
                }
                else if (status == OK) {
                    creep.memory.working = false
                }
            }
        }
    }
    /**
     *
     * @param creep
     * @returns
     */
    static loop(creep: Creep) {

        const TERMINAL_ENERGY_NUM = 10000

        if (!creep.memory.isStanBy) {
            const terminal = creep.room[STRUCTURE_TERMINAL]
            if (terminal) {
                creep.goTo(terminal.pos)
                if (creep.pos.isNearTo(terminal.pos)) {
                    creep.memory.isStanBy = true
                }
                return
            }
        }

        if (creep.ticksToLive < 50 && (creep.store[RESOURCE_GHODIUM_HYDRIDE] > 0 || creep.store[RESOURCE_GHODIUM_ACID] > 0 || creep.store[RESOURCE_ENERGY] > 0)) {
            creep.say('->storage')
            const storage = creep.room[STRUCTURE_STORAGE]
            if (storage) {
                if (creep.store[RESOURCE_GHODIUM_HYDRIDE] > 0) {
                    const status = creep.transfer(storage, RESOURCE_GHODIUM_HYDRIDE)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storage.pos)
                        return
                    }
                }
                if (creep.store[RESOURCE_GHODIUM_ACID] > 0) {
                    const status = creep.transfer(storage, RESOURCE_GHODIUM_ACID)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storage.pos)
                        return
                    }
                }
                else if (creep.store[RESOURCE_ENERGY] > 0) {
                    const status = creep.transfer(storage, RESOURCE_ENERGY)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storage.pos)
                        return
                    }
                }
            }
        }

        if (!creep.memory.working && (
            creep.store.getFreeCapacity(RESOURCE_ENERGY) > 0 ||
            creep.store.getFreeCapacity(RESOURCE_GHODIUM_HYDRIDE) > 0 ||
            creep.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 0
        ) && creep.ticksToLive > 50) {

            const storage = creep.room[STRUCTURE_STORAGE]
            if (!storage) {
                delete creep.memory.targetId
                creep.memory.working = false
                return
            }

            // lab
            const labs = creep.pos.findClosestByPath(FIND_STRUCTURES, {
                filter: s => s.structureType == STRUCTURE_LAB && (
                    s.store.getFreeCapacity(RESOURCE_ENERGY) > 0 ||
                    (s.store.getFreeCapacity(RESOURCE_GHODIUM_HYDRIDE) > 0 && storage.store[RESOURCE_GHODIUM_HYDRIDE] > 0) ||
                    (s.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 0 && storage.store[RESOURCE_GHODIUM_ACID] > 0)
                )
            }) as StructureWithStore
            if (labs) {
                if (labs.store.getFreeCapacity(RESOURCE_ENERGY) > 0 && storage.store[RESOURCE_ENERGY] > 0) {
                    const status = creep.withdraw(storage, RESOURCE_ENERGY)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storage.pos)
                    }
                    else if (status == OK) {
                        creep.say('->lab')
                        creep.memory.targetId = labs.id
                        creep.memory.working = true
                    }
                    return
                }
                else if (labs.store.getFreeCapacity(RESOURCE_GHODIUM_HYDRIDE) > 0 && storage.store[RESOURCE_GHODIUM_HYDRIDE] > 0) {
                    const status = creep.withdraw(storage, RESOURCE_GHODIUM_HYDRIDE)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storage.pos)
                    }
                    else if (status == OK) {
                        creep.say('->lab')
                        creep.memory.targetId = labs.id
                        creep.memory.working = true
                    }
                    return
                }
                else if (labs.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 0 && storage.store[RESOURCE_GHODIUM_ACID] > 0) {
                    const status = creep.withdraw(storage, RESOURCE_GHODIUM_ACID)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(storage.pos)
                    }
                    else if (status == OK) {
                        creep.say('->lab')
                        creep.memory.targetId = labs.id
                        creep.memory.working = true
                    }
                    return
                }
            }

            RoleCenterLabManager.sleep(creep)
        }
        else {
            const targetId = creep.memory.targetId
            if (targetId) {
                const target = Game.getObjectById(targetId) as StructureWithStore
                if (creep.store[RESOURCE_ENERGY] > 0) {
                    const status = creep.transfer(target, RESOURCE_ENERGY)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(target.pos)
                        return
                    }
                    else if (status == OK) {
                        delete creep.memory.targetId
                        creep.memory.working = false
                    }
                }
                if (creep.store[RESOURCE_GHODIUM_HYDRIDE] > 0) {
                    const status = creep.transfer(target, RESOURCE_GHODIUM_HYDRIDE)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(target.pos)
                        return
                    }
                    else if (status == OK) {
                        delete creep.memory.targetId
                        creep.memory.working = false
                    }
                }
                if (creep.store[RESOURCE_GHODIUM_ACID] > 0) {
                    const status = creep.transfer(target, RESOURCE_GHODIUM_ACID)
                    if (status == ERR_NOT_IN_RANGE) {
                        creep.goTo(target.pos)
                        return
                    }
                    else if (status == OK) {
                        delete creep.memory.targetId
                        creep.memory.working = false
                    }
                }
            }
            else {
                RoleCenterLabManager.sleep(creep)
            }
        }
    }
}

export default RoleCenterLabManager
